package com.imodia.checkoo.service.impl;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.hsq.commons.util.DateUtil;
import com.hsq.commons.util.db.GenericIbatisDao;
import com.hsq.commons.util.db.PageBean;
import com.hsq.commons.util.db.PageResult;
import com.hsq.commons.util.db.annotation.SqlSpace;
import com.imodia.checkoo.bean.CmsOfferJobBean;
import com.imodia.checkoo.service.CmsOfferJobService;

@SqlSpace(spaceName="cmsofferjob")
@Service("cmsOfferJobService")
public class CmsOfferJobServiceImpl extends GenericIbatisDao<CmsOfferJobBean, Integer> implements CmsOfferJobService {
	
	private static final Log log = LogFactory.getLog(CmsOfferJobServiceImpl.class);
	
	public void checked(int id){
		int checked = super.getSqlDao().getInt("select checked as id from cms_offer_job where id = "+id);
		if(checked!=1){
			checked = 1;
		}else{
			checked = 0;
		}
		String sql = "update cms_offer_job set checked = "+checked+" where id = "+id;
		super.getSqlDao().update(sql);
	}
	
	
	public PageResult<CmsOfferJobBean> selectPaginationBySql(PageBean page,CmsOfferJobBean offerJob){
		String sql = "select j.join_count,o.* from cms_offer_job o left join (select count(*) as join_count,job_id from cms_join_job group by job_id) j on j.job_id = o.id where 1=1 ";
		if(offerJob.getTitle() != null && offerJob.getTitle() .trim().length()>0){
			sql +=" and title like '%"+offerJob.getTitle() +"%'" ;
		}
		if(offerJob.getShowAll()==null || !offerJob.getShowAll().trim().equals("1")){
			sql+=" and ext_2 >='"+DateUtil.toString(new Date(), "yyyy.MM.dd")+"'";
		}
		sql += " order by id desc ";
		return super.getSqlDao().page(sql,CmsOfferJobBean.class,page);
	}
		

}